package com.itzhihao.controller;

import com.itzhihao.common.constant.ProductStatus;
import com.itzhihao.entity.Product;
import com.itzhihao.entity.vo.ProductVo;
import com.itzhihao.service.IProductService;
import com.itzhihao.service.IStoreService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {

    @Resource
    private IProductService productService;
    @Resource
    private IStoreService storeService;

    @PostMapping("/back/list")
    public TableDataInfo getBackList(@RequestBody PageDomain pageDomain,
                                     @RequestBody Product product){
        return productService.getBackList(pageDomain,product);
    }
    /**
     * 获取商店内的商品列表
     * @param pageDomain
     * @return
     */
    @PostMapping("/store/list")
    public TableDataInfo getList(@RequestBody PageDomain pageDomain,
                                 @RequestBody Product product){
        return productService.getList(pageDomain,product);
    }

    /**
     * 首页及搜索内容模块
     * @param pageDomain
     * @param product
     * @return
     */
    @PostMapping("/main/list")
    public TableDataInfo getMainList(@RequestBody PageDomain pageDomain,
                                     @RequestBody Product product){
        return productService.getMainList(pageDomain,product);
    }

    /**
     * todo 猜你喜欢模块，需要使用到协同算法
     * @param pageDomain
     * @return
     */
    @PostMapping("/likeList")
    public TableDataInfo getLikeList(@RequestBody PageDomain pageDomain){
        return null;
    }
    

    /**
     * 查看商店详情
     * @param id
     * @return
     */
    @GetMapping
    public AjaxResult getDetail(@RequestParam(value = "product_id") Long id){
        ProductVo pvo = productService.getDetail(id);
        return success(pvo);
    }

    @PutMapping
    public AjaxResult update(@RequestBody Product product){
        boolean isUpdate = productService.updateOrSave(product);
        if(isUpdate){
            return AjaxResult.success("更新成功");
        }else{
            return AjaxResult.error("更新失败，请重试");
        }
    }

    @DeleteMapping("/delete/{product_id}")
    public AjaxResult delete(@PathVariable(value = "product_id") Long id){
        boolean isDelete = productService.deleteById(id);
        if(isDelete){
            return AjaxResult.success("删除成功");
        }else{
            return AjaxResult.error("删除失败，请重试");
        }
    }

    /**
     * 提交商品
     * @param product
     * @return
     */
    @PostMapping
    public AjaxResult add(@RequestBody Product product){

        boolean isSave = productService.updateOrSave(product);
        if (isSave){
            return AjaxResult.success("保存成功");
        }else{
            return AjaxResult.error("保存失败");
        }
    }


    @PutMapping("/auditing")
    public AjaxResult auditing(@RequestBody Product product){
        if(product.getStatus() != 0){
            return error();
        }
        boolean isAudit = productService.updateProductStatus(product,ProductStatus.AUDITING.ordinal());
        if(isAudit){
            return success();
        }else{
            return error();
        }
    }
    @PutMapping("/passAudit")
    public AjaxResult passAudit(@RequestBody Product product){
        if(product.getStatus() != 0 && product.getStatus() != 1){
            return error();
        }
        boolean isAudit = productService.updateProductStatus(product,ProductStatus.PASS_AUDIT.ordinal());
        if(isAudit){
            return success();
        }else{
            return error();
        }
    }
    @PutMapping("/noPassAudit")
    public AjaxResult noPassAudit(@RequestBody Product product){
        if(product.getStatus() != 0 && product.getStatus() != 1){
            return error();
        }
        boolean isAudit = productService.updateProductStatus(product,ProductStatus.NOPASS_AUDIT.ordinal());
        if(isAudit){
            return success();
        }else{
            return error();
        }
    }
    @PutMapping("/takeUp")
    public AjaxResult takeUp(@RequestBody Product product){
        if(product.getStatus() != 2){
            return error();
        }
        boolean isAudit = productService.updateProductStatus(product,ProductStatus.TAKE_UP.ordinal());
        if(isAudit){
            return success();
        }else{
            return error();
        }
    }
    @PutMapping("/takeDown")
    public AjaxResult takeDown(@RequestBody Product product){
        if(product.getStatus() != 4){
            return error();
        }
        boolean isAudit = productService.updateProductStatus(product,ProductStatus.TAKE_DOWN.ordinal());
        if(isAudit){
            return success();
        }else{
            return error();
        }
    }
    @PutMapping("/mustTakeDown")
    public AjaxResult mustTakeDown(@RequestBody Product product){
        if(product.getStatus() != 5){
            return error();
        }
        boolean isAudit = productService.updateProductStatus(product,ProductStatus.MUST_TAKE_DOWN.ordinal());
        if(isAudit){
            return success();
        }else{
            return error();
        }
    }
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Delete("/delete/{ids}")
    public AjaxResult deleteBatch(@PathVariable String ids){
        Long[] idLongArr = Convert.toLongArray(ids);
        List<Long> idLongList = Arrays.asList(idLongArr);
        boolean isDelete = productService.deleteByIds(idLongList);
        if(isDelete){
            return success("批量删除成功");
        }else{
            return error("批量删除失败");
        }
    }



}
